var appSetting = getGameConfig();
init();

function init(){
    var wrapperNode = document.getElementById("he_canvas");
    if(appSetting["swap_type"] == "2"){
        if(location.host == "apps.facebook.com" && top==window){
            platform_action_facebook();
            return true;
        }
        if(!wrapperNode){
            return false;
        }else if(location.href.indexOf("facebooknew") == -1){
			overrideAppId();
		}
    }else{
        if(!wrapperNode){
            return false;
        }
        overrideAppConfig();
        overrideAppHeightUrl();
    }
}


function overrideAppId(){
    if(location.href.indexOf("facebook/v5/")>-1){
        return false;
    }

    var appId = appSetting['app_id'];
    appId = appId.replace(/(^\s*)|(\s*$)/g, "");
	var frameUrl = appSetting['frame_url'];
    var loc = location.href;
    var platforms = {
        "qq":"qzoneapp.com",
        "iframe":"happyelements.com"
    };
    for(var x in platforms){
        if(location.host.indexOf(platforms[x]) > -1){
            window['platform_action_'+x](appId, frameUrl);
            return true;
        }
    }

    //iframe style, such as mixi, cyworld, hangame    
    var scriptNodes = document.getElementsByTagName("script");
    for(var i=0; i<scriptNodes.length;i++){
        var scriptNode = scriptNodes[i];
        if(scriptNode.src.indexOf("preload.jsp") > -1){
            var newSrc = scriptNode.src.replace(/\d{10}/, appId);

            scriptNode.parentNode.removeChild(scriptNode);

            var newScriptNode = document.createElement("script");
            newScriptNode.src = newSrc;
            document.body.appendChild(newScriptNode);

            break;
        }
    }


}

function platform_action_facebook(){
    var appId = appSetting['app_id'];
    appId = appId.replace(/(^\s*)|(\s*$)/g, "");

    var forms = document.forms;
    for(var i=0;i<forms.length;i++){
        var formNode = forms[i];
        if(formNode.action.indexOf("gip.happyelements.com") > -1){
            formNode.action = formNode.action.replace(/\d{10}/, appId);
            formNode.submit();
            break;
        }
    }
}


function platform_action_qq(appId, frameUrl){
	if(frameUrl){
		location.href = frameUrl.replace("%s", appId) + location.search;
	}else{
		location.href = "http://"+location.host+"/gip-homepage/qzoneV3/v1/"+appId+"/"+location.search; 
	}
    
}

function platform_action_iframe(appId){
    if(location.href.indexOf("gip_app_id=") > -1){
        location.href = location.href.replace(/\d{10}/g, appId); 
    }else{
        location.href = location.href.replace(/\d{10}/, appId); 
    }
}

//override gameblock
function overrideAppConfig(){
    var gameBlock = appSetting["config"];
    var config = decodeURIComponent(Gip_JS_Config.gameBlock); 
    var arr = config.split("<config>");
    var arr2 = arr[arr.length-1].split("<apiStatus>")
    Gip_JS_Config.gameBlock = arr[0] + "<config>"+gameBlock+"<apiStatus>" + arr2[1];

}

//reset game height and game url, then render app again
function overrideAppHeightUrl(){
    var ex_gip_timer;
    ex_gip_timer= setInterval(function(){
        var swfObjects = document.getElementsByTagName("object");
        var swfObject;
        for(var i=0;i<swfObjects.length;i++){
            if(swfObjects[i].data.indexOf("gipcontainer") > -1){
                swfObject = swfObjects[i];
                break;
            }
        }
        if(swfObject){
            clearInterval(ex_gip_timer);

            var gameHeight = appSetting['height'];
            var gameUrl = appSetting['url'];

            swfObject.height = gameHeight;
            
            var paramNodes = swfObject.getElementsByTagName("param");
            for(var i=0;i<paramNodes.length;i++){
                var paramNode = paramNodes[i];
                if(paramNode.name == "height"){
                    paramNode.value = gameHeight;
                }
                if(paramNode.name == "flashvars"){
                    paramNode.value = changeFlashVars(paramNode.value, gameUrl);
                }
            }

            swfObject.parentNode.innerHTML = swfObject.parentNode.innerHTML;
        }
    }, 300);
}

function changeFlashVars(flashvars, gameUrl){
    var sep = "&";
    var arr = flashvars.split(sep);
    for(var i=0;i<arr.length;i++){
        var arr2 = arr[i].split("=");
        if(arr2[0] == "gameUrl"){
            arr[i] = arr2[0]+'='+gameUrl;
        }else{
            arr[i]=arr2.join("=");
        }
    }
    return arr.join(sep);
}

function getGameConfig(){
    var dataNode = document.getElementById("ex_hidden_app_setting");
    var dataObj = JSON.parse(dataNode.value);
    return dataObj;
}
